package com.itic.web.mem.cashorder.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.ht.mem.member.persistence.model.WebMember;
import com.itic.ht.mem.member.service.WebMemberService;
import com.itic.ht.mem.performance.persistence.model.Performance;
import com.itic.ht.mem.performance.service.PerformanceService;
import com.itic.ht.mem.withdraw.persistence.model.Withdraw;
import com.itic.ht.mem.withdraw.persistence.model.WithdrawExample;
import com.itic.ht.mem.withdraw.service.WithdrawService;
import com.itic.web.qo.TokenData;
import com.itic.web.ro.ResResult;
import com.itic.web.util.TokenUtil;


@Service
@Transactional
public class AppPerformanceService {
	
	@Autowired
	private WebMemberService webMemberService;
	@Autowired
	private PerformanceService performanceService;
	@Autowired
	private WithdrawService withdrawService;
	
	public ResResult getList(TokenData data) {
		try {
			// 判断token是不是存在
			if (StringHelper.isEmpty(data.getToken())) {
				return ResResult.error(1, "token不能为空");
			}
			// 先判断token正确性
			boolean flag = TokenUtil.assertUserToken(data);
			if (!flag) {
				return ResResult.error(2, "token失效");
			}
			Map<String, Object> resMap = new HashMap<>();
			// 从token中取account信息 
			data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
			// 取得用户信息
			WebMember member = webMemberService.getMemberByAccount(data.getAccount());
			//查询用户的业绩信息
			Performance performance = performanceService.getPerformanceByAccount(data.getAccount());
			//查询用户的提现记录
			WithdrawExample example = new WithdrawExample();
			example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andAccountEqualTo(data.getAccount());
			List<Withdraw> withdrawList = withdrawService.list(example);
			resMap.put("member", member);
			resMap.put("withdraw", withdrawList);
			resMap.put("performance", performance);
			return ResResult.build(200, "我的业绩查询成功", resMap);
		} catch (Exception e){
			return ResResult.error(4, "我的业绩查询失败");
		}
	}

	public ResResult applyNow(TokenData data, String price,String payAccount) {
		try {
			// 判断token是不是存在
			if (StringHelper.isEmpty(data.getToken())) {
				return ResResult.error(1, "token不能为空");
			}
			// 先判断token正确性
			boolean flag = TokenUtil.assertUserToken(data);
			if (!flag) {
				return ResResult.error(2, "token失效");
			}
			Map<String, Object> resMap = new HashMap<>();
			// 从token中取account信息 
			data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
			// 取得用户信息
			WebMember member = webMemberService.getMemberByAccount(data.getAccount());
			//根据用户信息取得业绩总额//取得业绩余额
			Performance performance = performanceService.getPerformanceByAccount(member.getAccount());
			if(null==performance){
				return ResResult.error(400, "您没有业绩信息");
			}
			if(Double.parseDouble(price)>member.getBalance().doubleValue()){
				return ResResult.error(401, "提现金额不能大于可提现金额");
			}
			//保存提现申请
			Withdraw withdraw = new Withdraw();
			withdraw.setId(IdGen.uuid());
			withdraw.setAccount(member.getAccount());
			withdraw.setBalance(member.getBalance().doubleValue()-Double.parseDouble(price));
			withdraw.setChangeAcount(Double.parseDouble(price));
			withdraw.setCtime(new Date());
			withdraw.setMtime(new Date());
			withdraw.setIsEffective(DBConst.TRUE);
			withdraw.setDrawAccount(payAccount);
			//提现方式（暂时只支持支付宝提现）
			withdraw.setPayType("1");
			withdraw.setStatus(DBConst.ZHIFUBAO_WITHDRAW);
			withdraw.setTotalAcount(performance.getIncome());
			//修改用户余额信息和增加提现记录
			member.setBalance(new BigDecimal(member.getBalance().doubleValue()-Double.parseDouble(price)));
			webMemberService.edit(member);
			withdrawService.add(withdraw);
			return ResResult.build(200, "提现申请已提交");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
